import os
import xml.dom.minidom
import cv2
from openpyxl import Workbook,load_workbook

base_PATH=os.getcwd()
PATH_images = os.path.abspath('./test_images')
PATH_images_to = os.path.abspath('./_test_images')
wb=load_workbook('stucsv_test.xlsx')
sn=wb.sheetnames
ws = wb[sn[0]]
global id
for hang in range(1,833+1):
    if hang==1:
        id=0
    elif hang>1:
        if ws.cell(row=hang, column=1).value==ws.cell(row=hang-1, column=1).value:
            id+=1
        elif ws.cell(row=hang, column=1).value!=ws.cell(row=hang-1, column=1).value:
            id=0
    name=ws.cell(row=hang, column=1).value
    str_name=str(name)
    image_path = (str_name + '.png')

    os.chdir(PATH_images)
    image = cv2.imread(image_path)


    x_min=ws.cell(row=hang, column=2).value
    y_min=ws.cell(row=hang, column=3).value
    x_max=ws.cell(row=hang, column=4).value
    y_max=ws.cell(row=hang, column=5).value

    cropped = image[int(y_min):int(y_max),
              int(x_min):int(x_max)]  # 裁剪坐标为[y0:y1, x0:x1]

    os.chdir(PATH_images_to)
    cv2.imwrite(str_name + '_' + str(id) + '.png', cropped)

